"""
Author: Faye Lee
Date: 2025-05-08 17:50:29
LastEditTime: 2025-05-09 11:50:56
LastEditors: Faye Lee
Description: 为了在屏幕上显示得分，我们创建了一个Scoreboard类
FilePath: \pygame_alien\scoreboard.py
Halo Baby,欢迎打开新世界的大门!
"""

import pygame.font
from pygame.sprite import Group
from ship import Ship


class Scoreboard:
    """显示得分信息的类"""

    def __init__(self, ai_settings, screen, stats,fps):
        """初始化显示得分涉及的属性"""
        self.screen = screen
        self.screen_rect = screen.get_rect()
        self.ai_settings = ai_settings
        self.text_color = ai_settings.score_text_color
        self.stats = stats
        self.fps = fps
        self.font = pygame.font.Font(
            ai_settings.top_font_path, ai_settings.top_font_size
        )

        # 准备初始得分图像和最高得分图像
        self.prep_score()
        self.prep_high_score()
        self.prep_level()
        self.prep_ships()
        self.prep_fps()

    def prep_score(self):
        """将得分转换为一幅渲染的图像"""

        self.check_high_score()
        rounded_score = int(round(self.stats.score, 0))
        score_str = "当前得分: " + str("{:,}".format(rounded_score)) + " 分"
        self.score_image = self.font.render(
            score_str, True, self.text_color, self.ai_settings.bg_color
        )

        # 将得分放在右上角
        self.score_image_rect = self.score_image.get_rect()
        self.score_image_rect.right = self.screen_rect.right - 20
        self.score_image_rect.top = 20

    def prep_high_score(self):
        """渲染最高得分"""
        high_score = int(round(self.stats.high_score, 0))
        high_score_str = "最高得分: " + str("{:,}".format(high_score)) + " 分"
        self.high_score_image = self.font.render(
            high_score_str, True, self.text_color, self.ai_settings.bg_color
        )

        # 将最高得分放在顶部中央
        self.high_score_image_rect = self.high_score_image.get_rect()
        self.high_score_image_rect.centerx = self.screen_rect.centerx
        self.high_score_image_rect.top = 20

    def prep_level(self):
        level_str = "当前等级: " + str(self.stats.level)
        self.level_image = self.font.render(
            level_str, True, self.text_color, self.ai_settings.bg_color
        )
        self.level_image_rect = self.level_image.get_rect()
        self.level_image_rect.left = self.screen_rect.left + 20
        self.level_image_rect.bottom = self.screen_rect.bottom - 20

    def prep_fps(self):
        fps_str = "FPS: " + str(int(self.fps.get_fps()))
        self.fps_image = self.font.render(
            fps_str, True, self.ai_settings.score_text_color, self.ai_settings.bg_color
        )
        self.fps_image_rect = self.fps_image.get_rect()
        self.fps_image_rect.right = self.screen_rect.right - 20
        self.fps_image_rect.bottom = self.screen_rect.bottom - 20

    def prep_ships(self):
        """显示还余下多少飞船"""
        self.ships = Group()
        for ship_number in range(self.stats.ships_lives):
            ship = Ship(self.ai_settings, self.screen,30)
            ship.rect.y = 10
            ship.rect.x = 10 + ship_number* ship.rect.width + (ship_number +1)*20
            self.ships.add(ship)


    def check_high_score(self):
        if self.stats.score > self.stats.high_score:
            self.stats.high_score = self.stats.score
            self.prep_high_score()
            self.text_color = self.ai_settings.high_text_color
        else:
            self.text_color = self.ai_settings.score_text_color

    def show_score(self):
        """在屏幕上显示得分"""
        self.screen.blit(self.score_image, self.score_image_rect)
        self.screen.blit(self.high_score_image, self.high_score_image_rect)
        self.screen.blit(self.level_image, self.level_image_rect)
        self.ships.draw(self.screen)
        self.screen.blit(self.fps_image, self.fps_image_rect)
